package server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import server.to.MemberTO;
import server.to.UpdateTo;

public class ServerDao {
	
	private BasicDataSource dataSource = new BasicDataSource();
	
	public ServerDao(){
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
		dataSource.setUsername("cac");
		dataSource.setPassword("tiger");
	}
	
	public void insertMember(MemberTO mto) throws SQLException{
		Connection connection = null;
		PreparedStatement pstmt = null;
		String sql = "insert into cac_member values (?, ?, ?)";
		try{
			connection = dataSource.getConnection();
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, mto.getMemberId());
			pstmt.setString(2, mto.getMemberPassword());
			pstmt.setString(3, mto.getMemberName());
			pstmt.execute();
		}finally{
			if(pstmt!=null){
				pstmt.close();
			}
			if(connection!=null){
				connection.close();
			}
		}
	}
	
	//updatePool 관련
	public ArrayList<UpdateTo> selectRecentUpdatePool(String updateDate) throws SQLException{
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		ArrayList<UpdateTo> list = new ArrayList<UpdateTo>();
		String sql = "select directory_path, update_date from update_pool where update_date=?";
		try{
			connection = dataSource.getConnection();
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, updateDate);
			rset = pstmt.executeQuery();
			while(rset.next()){
				list.add(new UpdateTo(rset.getString(1), rset.getString(2)));
			}
		}finally{
			if(rset!=null){
				rset.close();
			}
			if(pstmt!=null){
				pstmt.close();
			}
			if(connection!=null){
				connection.close();
			}
		}
		return list; 
	}
}
